{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 简单线性回归（最小二乘法）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 0. 引入依赖"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. 导入数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD4CAYAAAAXUaZHAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+j8jraAAAauUlEQVR4nO3df4xdZZ3H8feX6SAD6zoCEwJTamsk06gVihPEdGOk6BaFQFONQmQXlU3/WLPrurvVNmvWZaPbmu4G2WxW08UfbHChWGrxx8ZKKMaECGS6BSs/uiBI6fCjNVA2wgjT9rt/3HPp/Lg/zr3n13PO+bySZuaeuXPnuWc63/M83+f7PMfcHRERqZYTim6AiIikT8FdRKSCFNxFRCpIwV1EpIIU3EVEKmhB0Q0AOP30033x4sVFN0NEpFR27979W3cfafW1IIL74sWLmZiYKLoZIiKlYmZPtfua0jIiIhWk4C4iUkFdg7uZfcvMDprZr2Yc22xmj5rZL83s+2Y2PONrG8zscTPbZ2arsmq4iIi0F6fn/h3gkjnH7gTe6e7vAv4X2ABgZm8HrgTeEX3Pv5vZQGqtFRGRWLoGd3f/OfDCnGM/dfcj0cN7gYXR51cAt7r7q+7+JPA4cEGK7RURkRjSqJb5NLA1+nyURrBvOhAdm8fM1gJrARYtWpRCM0RE4tmxZ5LNO/fxzOEpzhoeYt2qMVYvbxmqSivRhKqZ/R1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sNmts3MHjWzR8zsvXU8H2Y2Fv2faP77PzP7q6zPhYJ7714FVrr7ucB5wCVmdiHwVeB6d38b8CJwbYFtzNtngUdmPK7zubjI3c+bUcO8HrjL3c8B7ooe18UNwE/cfSlwLo3/I7U7H+6+L/o/cR7wbuAV4PtkfC4U3HvkDb+LHg5G/xxYCWyLjt8ErC6gebkzs4XApcCN0WOjpueijStonAOo0bkwszcB7wO+CeDur7n7YWp6Pma4GPi1uz9FxudCwb0PURriAeAgcCfwa+Cwux+JnnIAqMsdOL4GfB44Fj0+jfqeCwd+ama7zWxtdOwMd382+vw54Iximpa7JcAh4NtRyu5GMzuF+p6PpiuBW6LPMz0XCu59cPej0RBrIXABsLTgJhXCzC4DDrr77qLbEog/cvfzgQ8BnzGz9838ojfqjutSe7wAOB/4ursvB15mTtqhZueDaO7pcuB7c7+WxblQcE8gGmbeDbwXGDaz5m0LFwKThTUsPyuAy83sN8CtNNIxN1DPc4G7T0YfD9LIqV4APG9mZwJEHw8W18JcHQAOuPt90eNtNIJ9Xc8HNC76/+Puz0ePMz0XCu49MrMRMxuOPh8CPkhjouhu4KPR064B7iimhflx9w3uvtDdF9MYbu5y909Qw3NhZqeY2RubnwN/DPwK+AGNcwA1ORcA7v4c8LSZjUWHLgYepqbnI3IVx1MykPG50ArVHpnZu2hMfgzQuDje5u7/aGZvpdF7PRXYA1zt7q8W19J8mdn7gb9198vqeC6i9/z96OEC4L/c/StmdhpwG7CIxrbWH3P3FwpqZq7M7DwaE+0nAk8AnyL6m6Fm5yO64O8H3uruL0XHMv2/oeAuIlJBSsuIiFSQgruISAUpuIuIVJCCu4hIBSm4i4hUkIK7iEgFKbiLiFTQ/wN8F0LjPjEAqgAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "points = np.genfromtxt('data.csv', delimiter=',')\n",
    "\n",
    "# 提取 points 中的两列数据， 分别为 x, y\n",
    "x = points[:, 0]\n",
    "y = points[:, 1]\n",
    "\n",
    "# 用 plt 画出散点图\n",
    "plt.scatter(x, y)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. 定义损失函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 损失函数是系数的函数，另外还需要传入数据的 x,y\n",
    "def compute_cost(w, b, points):\n",
    "    total_cost = 0\n",
    "    M = len(points)\n",
    "\n",
    "    # 逐点计算平方损失误差，然后求平均数\n",
    "    for i in range(M):\n",
    "        x = points[i, 0]\n",
    "        y = points[i, 1]\n",
    "        total_cost += ( y - w * x - b ) ** 2\n",
    "  \n",
    "    return total_cost/M"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. 定义模型的超参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "alpha = 0.0001\n",
    "initial_w = 0\n",
    "initial_b = 0\n",
    "num_iter = 10"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4. 定义核心梯度下降算法函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "def grad_desc(points, initial_w, initial_b, alpha, num_iter):\n",
    "    w = initial_w\n",
    "    b = initial_b\n",
    "    # 定义一个 list 保存所有的损失函数值，用来显示下降的过程\n",
    "    cost_list = []\n",
    "    for i in range(num_iter):\n",
    "        cost_list.append(compute_cost(w, b, points))\n",
    "        w, b = step_grad_desc(w, b, alpha, points)\n",
    "    \n",
    "    return [w, b, cost_list]\n",
    "\n",
    "def step_grad_desc(current_w, current_b, alpha, points):\n",
    "    sum_grad_w = 0\n",
    "    sum_grad_b = 0\n",
    "    M = len(points)\n",
    "    \n",
    "    # 对每个点，代入公式\n",
    "    for i in range(M):\n",
    "        x = points[i, 0]\n",
    "        y = points[i, 1]\n",
    "        sum_grad_w += (current_w * x + current_b - y) * x\n",
    "        sum_grad_b += current_w * x + current_b -y\n",
    "        \n",
    "    # 用公式求当前梯度\n",
    "    grad_w = 2/M * sum_grad_w\n",
    "    grad_b = 2/M * sum_grad_b\n",
    "    \n",
    "    # 梯度下降，更新当前的w和b\n",
    "    updated_w = current_w - alpha * grad_w\n",
    "    updated_b = current_b - alpha * grad_b\n",
    "    \n",
    "    return updated_w, updated_b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5. 测试： 运行梯度下降算法计算最优的 w 和 b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "w is:  1.4774173755483797\n",
      "b is:  0.02963934787473238\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX0AAAD4CAYAAAAAczaOAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+j8jraAAAcBUlEQVR4nO3dfXRcd33n8fd3Rg/Wgy2NHnD8II9CYoMd0hhpYsIChhLIA7ANpwfYUJYY8J60ELp0S7eb0HZzCuVsKJxSOAuckxKD01Iom8Ihy0kTcgJtSJc8SCJPthNbJH5WYlmWFMt61nz3j7lyxrZkSdZo7szcz+vUZ+787r0z3xmaz7363d/9jbk7IiISDbGwCxARkfxR6IuIRIhCX0QkQhT6IiIRotAXEYmQsrALOJ+mpiZvbW0NuwwRkaLS2dl53N2bZ1pX0KHf2tpKR0dH2GWIiBQVMzsw2zp174iIRIhCX0QkQhT6IiIRotAXEYkQhb6ISIQo9EVEIkShLyISISUZ+kcGRvjyA89xZGAk7FJERApKSYb+0Ogk3/jFb3j0N31hlyIiUlBKMvTXv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bgLwN3H3X0g3KpCVwZUmVkZUA0cDbmenCvF0J/zh1qiysxagTcCj4VbSaj+FvhTIB12IQXgYqAX+E7Q3fVtM6sJu6iwuPsR4CvAQaAHGHT3n4VbVe6VYujLDMysFvhn4I/c/ZWw6wmDmb0POObunWHXUiDKgDbgW+7+RuAUENlrYGaWINMrcDGwGqgxs/8cblW5V4qhrx9qOYuZlZMJ/O+5+4/CridEbwF+x8z2k+n2e6eZ/UO4JYXqMHDY3af/8ruHzEEgqt4FvOjuve4+AfwI+A8h15RzpRj6+qGWLGZmZPps97j734RdT5jc/TZ3X+vurWT+/+Ln7l5yZ3Lz5e4vAYfM7HVB09XA7hBLCttB4Cozqw7+u7maErywnff59JeafqjlHG8BPgo8Y2ZPBm2fC37XQOQPge8FJ0gvAB8PuZ7QuPtjZnYP0EVm1NuvKcEpGTQNg4hIhJRi946IiMxCoS8iEiEKfRGRCFHoi4hEiEJfRCRCFPoiIhGi0BcRiZD/DwcZmpxW2DUpAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "w, b, cost_list = grad_desc(points, initial_w, initial_b, alpha, num_iter)\n",
    "\n",
    "print(\"w is: \", w)\n",
    "print(\"b is: \", b)\n",
    "cost = compute_cost(w, b, points)\n",
    "\n",
    "print('cost is: ', cost)\n",
    "plt.plot(cost_list)\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
